import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet( "/own-Servlet")
public class ownServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ss=request.getParameter("own");

       //先清空
        File file =new File("own.txt");
        try {
            if(!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter =new FileWriter(file);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            File writeName = new File("own.txt"); // 相对路径，如果没有则要建立一个新的output.txt文件
            try (FileWriter writer1 = new FileWriter(writeName,true);
                 BufferedWriter out = new BufferedWriter(writer1)
            ) {
                out.write(ss);
                out.flush(); // 把缓存区内容压入文件
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        response. sendRedirect("/userInfo.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
